import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  // 主页
  {
    path:"/",
    component:()=>import('../views/index.vue')
  },
  // 登录
  {
    path:"/login",
    component:()=>import('../views/login.vue')
  },
  // 注册
  {
    path:"/regist",
    component:()=>import('../views/regist.vue')
  },
  // 分类
  {
    path:"/class",
    component:()=>import('../components/class.vue')
  },
  // 个人中心
  {
    path:"/me",
    component:()=>import('../components/Me.vue')
  },
  // 我的账户
  {
    path:"/meaccount",
    component:()=>import('../components/Me_account.vue'),
    meta: {
      metalogin: true
    }
  },
  // 购物车
  {
    path:'/cart',
    component:()=>import('../components/cart.vue'),
    meta: {
      metalogin: true
    }
  },
  // 搜索页
  {
    // 开始
    path:'/start_search',
    component:()=>import('../components/start_search.vue')
  },
  {
    // 结束
    path:'/end_search',
    component:()=>import('../components/end_search.vue')
  },
  // 收获地址
  {
    path:'/address',
    component:()=>import('../components/address.vue'),
    meta: {
      metalogin: true
    }
  },
  // 添加地址
  {
    path:'/address_edit',
    component:()=>import('../components/address_edit.vue'),
    meta: {
      metalogin: true
    }
  },
  // 订单
  {
    path:'/order',
    component:()=>import('../components/order.vue'),
    meta: {
      metalogin: true
    }
  },
  // 订单确认
  {
    path:'/orderconfirm',
    component:()=>import('../components/order_confirm.vue'),
    meta: {
      metalogin: true
    }
  },
  // 商品详情
  {
    path:'/shopdetails',
    component:()=>import('../components/shop_details.vue')
  },
  // 分类子页面
  {
    path:'/class_children',
    component:()=>import('../components/class_children.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 添加路由导航
router.beforeEach((to, from, next) => {
  // 判断要去的路由中meta是否为undefined和meta.metalogin是否为undefined和meta.metalogin为true
  if (to.meta !== undefined && to.meta.metalogin !== undefined && to.meta.metalogin) {
    let token = localStorage.getItem('token')
    if (token === null) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})
export default router
